//
//  YCGyroPushViewController.m
//  DemoGyro
//
//  Created by 唐可雄 on 16/8/31.
//  Copyright © 2016年 swustyc. All rights reserved.
//

#import "YCGyroPushViewController.h"

#import <CoreMotion/CoreMotion.h>

@interface YCGyroPushViewController ()

/// 传感器管理器
@property (strong, nonatomic) CMMotionManager *manager;

@end

@implementation YCGyroPushViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if (self.manager.isGyroAvailable) {
        // 陀螺仪可用
        [self.manager startGyroUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {
            // 当采样到陀螺仪信息时就会执行
            if (error) {
                return;
            }
            
            CMRotationRate rate = gyroData.rotationRate;
            self.textView.text = [self.textView.text stringByAppendingString:[NSString stringWithFormat:@"x:%f y:%f z:%f\n", rate.x, rate.y, rate.z]];
        }];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"陀螺仪不可用" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Setter and Getter

- (CMMotionManager *)manager {
    if (_manager) {
        _manager = [[CMMotionManager alloc] init];
        
        // 1秒采集1次
        _manager.gyroUpdateInterval = 1./1.;
    }
    
    return _manager;
}

#pragma mark - IBAction

- (IBAction)clearBtnDidClick:(id)sender {
    self.textView.text = nil;
}

@end
